#define _CRTDBG_MAP_ALLOC
#define _CRT_SECURE_NO_WARNINGS

#include <stdlib.h>
#include <crtdbg.h>

#include <GL\glew.h>
#include <GL\freeglut.h>

#include "science\science.h"

equation_one_variable_t eq;

float linearFun(float x) { return 3 + 2 * x; }

static void init()
{
	eq = create_equation_one_variable(linearFun);
}

static void reshape(int sx, int sy)
{
	// set the viewport of the opengl window
	glViewport(0, 0, sx, sy);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	GLfloat aspectRatio = (GLfloat)sx / (GLfloat)sy; 
	if (sx >= sy)       
		gluOrtho2D (-1.0, 1.0, (-1.0 / aspectRatio), (1.0 / aspectRatio));
	else       
		gluOrtho2D (-1.0 * aspectRatio, 1.0 * aspectRatio, -1.0, 1.0);
}

static void display()
{	
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable( GL_POINT_SMOOTH );
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

	graph_equation_one_variable(eq, create_closed_closed_interval(-10, 10), .1, 1.0, 0.0, 1.0, 1.0);

	glFlush();
	glutSwapBuffers();
}

int main(int argc, char** argv)
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(600, 600);
	glutInitContextVersion(4, 4);
	glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);	
	glutCreateWindow(argv[0]);
	if (glewInit()) 
		exit(EXIT_FAILURE);

	init();
	glutReshapeFunc(reshape);
	glutDisplayFunc(display);
	glutMainLoop();

	return 0;
}